package com.gy.controller;


import com.gy.pojo.User;
import com.gy.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    BaoXiuService baoXiuService;
    @Autowired
    LeiXingService leiXingService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    UserService userService;
    @Autowired
    QuYuService quYuService;

    //注册用户
    @RequestMapping("/register/adduser")
    public String adduser(){


        return "user2/adduser";
    }
    //注册成功跳转到登录页面
    @RequestMapping("/register/adduseryse")
    public String adduseryes(String username,String pwd,Model model){

        //验证该用户是否存在
        if (userService.selectpwd(username)!=null){
            model.addAttribute("erro","该用户名已存在");
            return "user2/adduser";
        }else {

            userService.addUser(username,pwd);
            model.addAttribute("zhuce","注册成功 请登录");
            return "login";
        }

    }

    //去修改密码页面
    @RequestMapping("/all/updatepwd/{name}")
    public String updatepwd(@PathVariable("name")String name, Model model){
        model.addAttribute("username",name);

        return "user2/updatepwd";
    }

    //修改密码
    @PostMapping("/all/updatepwd")
    public String update(String username, String pwd, String newpwd,Model model){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);

        //拿到数据进行验证登录
        try {
            subject.login(token);//这个方法就是执行登录方法  不成功会抛出异常  用户名不对  密码不对
            userService.updatepwd(username,newpwd);
            model.addAttribute("again", "您已成功修改密码，请重新登录");
            return "login";
        } catch (UnknownAccountException e) {
            model.addAttribute("NoUser", "用户名或密码不正确");
            return "/user2/updatepwd"; //
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("NoPwd", "用户名或密码不正确");
            return "/user2/updatepwd";
        }

    }

    //查看用户
    @RequestMapping("/admin/user")
    public String user(Model model){
        List<User> users = userService.allUser();
        model.addAttribute("alluser",users);
        return "user2/selectuser";
    }
    //注销用户
    @RequestMapping("/admin/deleteuser/{id}")
    public String deleteuser(@PathVariable("id")int id){
       userService.deleteuser(id);

        return "redirect:/admin/user";
    }

    //管理用户权限
    //来到用户权限页面
    @RequestMapping("/admin/userperm")
    public String userperm(Model model){
        List<User> users = userService.allUser();
        model.addAttribute("alluser",users);

        return "user2/userperm";
    }

    //将权限改为管理员
    @RequestMapping("/admin/userpermAdmin/{id}")
    public String permadmin(@PathVariable("id")int id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("perm","user:admin");
        userService.updatePerm(map);

        return "redirect:/admin/userperm";
    }
    //将权限改为维修人员
    @RequestMapping("/admin/userpermWorker/{id}")
    public String workperm(@PathVariable("id")int id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("perm","user:work");
        userService.updatePerm(map);

        return "redirect:/admin/userperm";
    }
    //将权限改为学生
    @RequestMapping("/admin/userpermStudent/{id}")
    public String updateperm(@PathVariable("id")int id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("perm","user:stu");
        userService.updatePerm(map);

        return "redirect:/admin/userperm";
    }

















}
